package flc.ast.adapter;

import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import flc.ast.R;
import flc.ast.bean.WatchedBean;
import me.zhouzhuo.zzratingbar.ZzRatingBar;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;
import stark.common.bean.StkResBean;

public class WatchedAdapter extends StkProviderMultiAdapter<WatchedBean> {

    public WatchedAdapter() {
        addItemProvider(new StkSingleSpanProvider(126));
        addItemProvider(new WatchedProvider());
    }

    private class WatchedProvider extends BaseItemProvider<WatchedBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_watched;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, WatchedBean bean) {

            Glide.with(baseViewHolder.itemView).load(bean.getBean().getThumbUrl())
                    .into((ImageView) baseViewHolder.getView(R.id.ivWatchedItemImg));
            baseViewHolder.setText(R.id.tvWatchedItemName, bean.getBean().getName());
            baseViewHolder.setText(R.id.tvWatchedItemDate, bean.getDate());

            String str = new Gson().toJson(bean.getBean().getExtra());

            try {
                if (str != null && !str.equals("\"\"")) {
                    JSONObject extra = new JSONObject(str);
                    baseViewHolder.setText(R.id.tvWatchedItemReleaseDate, extra.optString("releaseDate"));
                }
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }

            String tag = getContext().getString(R.string.type_text);
            for (String s : bean.getBean().getTagNameList()) {
                tag += s + " / ";
            }
            baseViewHolder.setText(R.id.tvWatchedItemType, tag);

            //评分
            double score = (double) bean.getBean().getScore_total() / (bean.getBean().getScore_count() == 0 ? 1 : bean.getBean().getScore_count());
            String formattedNumber = String.format("%.1f", score);
            baseViewHolder.setText(R.id.tvWatchedItemScore, formattedNumber);

            ZzRatingBar zzRatingBar = baseViewHolder.getView(R.id.zzratingbar);
            if (score > 8) {
                zzRatingBar.setRating(5);
            } else if (score > 6) {
                zzRatingBar.setRating(4);
            } else if (score > 4) {
                zzRatingBar.setRating(3);
            } else if (score > 2) {
                zzRatingBar.setRating(2);
            } else {
                zzRatingBar.setRating(1);
            }
        }
    }

}
